import { ExtractJwt, Strategy } from 'passport-jwt';
import { PassportStrategy } from '@nestjs/passport';
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { IConfig } from 'src/config/config';

@Injectable()
export class AppStrategy extends PassportStrategy(Strategy,'app-jwt') {
	constructor(
		private readonly configService:ConfigService<IConfig>
	) {
		super({
			jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
			ignoreExpiration: false,
			secretOrKeyProvider: async (request, rawJwtToken, done) => {
				try {
					const secret = this.configService.get<IConfig['jwt']>('jwt').secret;
					done(null, secret);
				} catch (error) {
					done(error);
				}
			}
		});
	}

	async validate(payload: any) {
		return payload
	}
}
